package com.example.heathy_me.utils;

import android.os.StrictMode;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RequestHelper {
    private String json="";

    public void  getData(final String Url){

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
                    Request request = new Request.Builder()
                            .url(Url)//请求接口。如果需要传参拼接到接口后面。
                            .build();//创建Request 对象
                    Response response = null;
                    response = client.newCall(request).execute();//得到Response 对象
                    if (response.isSuccessful()) {
                        Log.d("kwwl","response.code()=="+response.code());
                        Log.d("kwwl","response.message()=="+response.message());
                        //此时的代码执行在子线程，修改UI的操作请使用handler跳转到UI线程。
                        String jsonData = response.body().string();
                        json = jsonData;
                        Log.d("kwwl","res=="+jsonData);
                    }
                    else {
                        json = "";
                    }
                } catch (Exception e) {
                    json = "";
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public User JSONtoUser(String jsonData) {
        //使用轻量级的Gson解析得到的json
        Gson gson = new Gson();
        User result = gson.fromJson(jsonData, User.class);
        Log.d("MainActivity", "avatar:" + result.getAvatar());
        Log.d("MainActivity", "id:" + result.getId());
        Log.d("MainActivity", "nick:" + result.getNick());
        Log.d("MainActivity", "password:" + result.getPassword());
        Log.d("MainActivity", "time:" + result.getTime());
        Log.d("MainActivity", "username:" + result.getUsername());
        return result;
    }

    public String run(String url) throws IOException {
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = client.newCall(request).execute()) {
            Log.d("kwwl","response.code()=="+response.code());
            Log.d("kwwl","response.message()=="+response.message());
            return response.body().string();
        }
    }


    public static final MediaType JSON
            = MediaType.get("application/json; charset=utf-8");
    public String register(String url, String json) throws IOException {
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

    public String friend_post(String url, String json) throws IOException {
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }
    public String friend_get(String url) throws IOException {
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = client.newCall(request).execute()) {
            Log.d("kwwl","response.code()=="+response.code());
            Log.d("kwwl","response.message()=="+response.message());
            return response.body().string();
        }
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }
}
